
class Cache {
	constructor(){}
  objectValue<T>(key: string): Promise<T | null> {
		return new Promise((resolve,_)=>{
			uni.getStorage({
				key:key,
				success(res) {
					if (res.data) {
						resolve(JSON.parse(res.data));
					}else{
						resolve(null);
					}
				},
				fail() {
					resolve(null);
				}
			})
		});
  }
  boolValue(key: string): Promise<boolean> {
    return new Promise((resolve,_)=>{
    	uni.getStorage({
    		key:key,
    		success(res) {
					let result = res.data
    			if (typeof result == "string" && result == "true") {
    				resolve(true);
    			}else if(typeof result == "boolean"){
    				resolve(result);
    			}else if(typeof result == "number"){
    				resolve(result > 0);
    			}else{
						resolve(false);
					}
    		},
    		fail() {
    			resolve(false);
    		}
    	})
    });
  }
  numberValue(key: string): Promise<number | null> {
    return new Promise((resolve,_)=>{
    	uni.getStorage({
    		key:key,
    		success(res) {
    			let result = res.data
    			if (typeof result == "string") {
    				resolve(parseInt(result));
    			}else if(typeof result == "number"){
    				resolve(result);
    			}else{
    				resolve(null);
    			}
    		},
    		fail() {
    			resolve(null);
    		}
    	})
    });
  }
  stringValue(key: string): Promise<string | null> {
    return new Promise((resolve,_)=>{
    	uni.getStorage({
    		key:key,
    		success(res) {
    			let result = res.data
    			resolve(result);
    		},
    		fail() {
    			resolve(null);
    		}
    	})
    });
  }

  set(value: any, key: string): Promise<boolean> {
	  let data:any = value
	  if (typeof value == "object") {
		  data = JSON.stringify(value)
	  }
	 return new Promise<boolean>((resole,reject)=>{
		 uni.setStorage({
		 	key:key,
		 		data:data,
		 		success() {
		 			resole(true)
		 		},fail() {
		 			reject(false)
		 		}
		 })
	 })
  }

	remove(key: string): Promise<boolean> {
		return new Promise<boolean>((resole, reject) => {
			uni.removeStorage({
				key: key,
				success() {
					resole(true)
				}, fail() {
					reject(false)
				}
			})
		})
	}
  removeAll(){
    uni.clearStorageSync()
  }
}
export let cache = new Cache();
